package com.hluck.goodweatherapp.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.hluck.goodweatherapp.R;
import com.hluck.goodweatherapp.data.bean.WeekWeather;
import com.hluck.goodweatherapp.databinding.ItemDailyRvBinding;
import com.hluck.goodweatherapp.utils.EasyDate;
import com.hluck.goodweatherapp.utils.WeatherUtil;

import java.util.List;

/**
 * @Author： LJH
 * @Time： 2024/1/3
 * @description：
 */
public class DailyAdapter extends RecyclerView.Adapter<DailyAdapter.ViewHolder> {

    private OnClickItemCallback onClickItemCallback;

    private final List<WeekWeather.DailyWeather> dailyWeathers;

    public DailyAdapter(List<WeekWeather.DailyWeather> dailyWeathers) {
        this.dailyWeathers = dailyWeathers;
    }

    public void setOnClickItemCallback(OnClickItemCallback onClickItemCallback) {
        this.onClickItemCallback = onClickItemCallback;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_daily_rv, parent, false);
        ItemDailyRvBinding binding = ItemDailyRvBinding.inflate(LayoutInflater.from(parent.getContext()),parent,false);
        ViewHolder viewHolder = new ViewHolder(binding);
        //添加点击回调
        binding.getRoot().setOnClickListener(v -> {
            if (onClickItemCallback != null){
                onClickItemCallback.onItemClick(viewHolder.getAdapterPosition());
            }
        });
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(DailyAdapter.ViewHolder holder, int position) {
        WeekWeather.DailyWeather dailyWeather = dailyWeathers.get(position);
//        Log.e("TAG","binding is null :"+(holder.binding == null));
        holder.binding.tvDate.setText(EasyDate.dateSplit(dailyWeather.getFxDate()) + EasyDate.getDayInfo(dailyWeather.getFxDate()));
        WeatherUtil.changeIcon(holder.binding.ivStatus,Integer.parseInt(dailyWeather.getIconDay()));
        holder.binding.tvHeight.setText(dailyWeather.getTempMax()+"℃");
        holder.binding.tvLow.setText(" / "+dailyWeather.getTempMin()+"℃");
    }

    @Override
    public int getItemCount() {
        return dailyWeathers.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        public ItemDailyRvBinding binding;

        public ViewHolder(ItemDailyRvBinding mbinding) {
            super(mbinding.getRoot());
            binding = mbinding;
        }
    }
}
